<?php
session_start();
if(isset($_SESSION['admin_id'])) {
    include "../../include/dbFunctions.php";
    function deleteClass($ID) {
        $id = $ID;
        //get file name
        $query0 = "SELECT class_student_list FROM class WHERE class_id='".$id."'";
        $result0 = executeSELECTQuery($query0);
        $file = "../../upload/".$result0[0]['class_student_list'];
        //header("Location:".$file);
        //delete file

        if (unlink($file)) {
            $_SESSION['mes']='File has been removed sucessfully';
        }
        //delete trainer
        $query3 = "UPDATE trainer SET class_id = null, trainer_status = 'accept' WHERE class_id= $id";
        $result3 = executeQuery($query3);
        //delete class
        $query1 = "DELETE FROM class WHERE class_id= $id";
        $result1 = executeQuery($query1);
        return $result1;
    }
    $id = $_GET['id'];
    //delete trainer
    $result1 = deleteClass($id);
    if ($result1!=0) {
        $_SESSION['mes'] = "Selected events has been deleted successfully";
        //update event status
        $query2= "SELECT volunteer_id FROM trainer WHERE event_id = $vid AND trainer_status = 'confirmed'";
        $trainer_array = executeSelectQuery($query2);
        $query3= "SELECT (class_id) FROM class WHERE event_id = $vid ";
        $class = executeSelectQuery($query3);
        $no_ready = count($trainer_array);
        $trainer_required = count($class)*2;
        if ($no_ready==$trainer_required) {
            $query = "UPDATE event
            SET event_status = 'confirmed'
            WHERE event_id = $eid";
            $result = executeQuery($query);
        } 
    } else {
        $_SESSION['mes'] = "Error! Please try again";
    }
    $id = $_SESSION['eid'];
    header('Location: ../eventView.php?id='.$id);
}else {
    header("Location: ../include/login.html");
}
?>